﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FlowgraphViewer.Model;
using System.Xml.Linq;
using System.Linq;
using FlowgraphViewer.Services.Extension_methods;
using FlowgraphViewer.Services.Parsers;
using System.Xml;

namespace FlowgraphViewer.Services
{
    public class FlowgraphXmlParser
    {
        private FlowgraphXmlParser()
        {
        }

        public static Flowgraph GetFlowgraph(string xmlContents)
        {
            try
            {
                XDocument doc = XDocument.Parse(xmlContents);

                var nodes = from node in doc.Descendants("Node")
                            select new FlowNodeInstance
                            {
                                Id = Int32.Parse(node.Attribute("Id").Value),
                                Flags = Int32.Parse(node.Attribute("flags").Value),
                                Position = PointParser.Parse(node.Attribute("pos").Value),
                                FlowNode = FlowNodeParser.Parse(node.Attribute("Class").Value),
                                Raw = node
                            };


                var fg = from graph in doc.Descendants("Graph")
                         select new Flowgraph()
                         {
                             Description = graph.Attribute("Description").Value,
                             Group = graph.Attribute("Group").Value,
                             Enabled = graph.Attribute("enabled").Value.Equals("1"),
                             Multiplayer = (EFlowgraphMultiplayer)Enum.Parse(typeof(EFlowgraphMultiplayer), graph.Attribute("MultiPlayer").Value, true)
                         };


                var edges = from edge in doc.Descendants("Edge")
                            select new Edge
                            {
                                Enabled = edge.Attribute("enabled").Value.Equals("1"),
                            };

                Flowgraph flowgraph = fg.First();

                foreach (var item in edges)
                {
                    flowgraph.Edges.Add(item);
                }

                foreach (var item in nodes)
                {
                    FillInputs(item);
                    flowgraph.Nodes.Add(item);
                }

                return flowgraph;
            }
            catch (XmlException)
            {
                return null;
            }

            
        }

        private static void FillInputs(FlowNodeInstance item)
        {
            var inputs = item.Raw.Element("Inputs");

            if (inputs != null)
            {
                foreach (var attr in inputs.Attributes())
                {
                    item.Inputs.Add(item.FlowNode.Inputs.Where(nodeitem => nodeitem.TechnicalName.Equals(attr.Name.LocalName)).First(), attr.Value);
                }
            }
        }

    }
}
